#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : deletindexingfile.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/5/20 9:30                 #
#                                                                   #
#                   @Last Update  : 2021/5/20 9:30                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:删除标引记录，重新标引                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
import os

import pymysql

from Seetings import JGMYSQL_CONNEXT


def delindexing(sysid, dbname, fieldname):
    """
    删除对应字段的标引结果
    :param sysid: 记录id
    :param dbname: 要删除的数据库名
    :param fieldname: 要删除的标引字段
    :return:
    """
    taskname = "`taskrecord`"
    DBName = {"`law`": "LAW", "`case`": "CASE"}
    # 加工数据库连接
    jg_conn = pymysql.connect(host=JGMYSQL_CONNEXT["MYSQL_HOST"],
                              port=JGMYSQL_CONNEXT["MYSQL_PORT"],
                              db=JGMYSQL_CONNEXT["MYSQL_DB"],
                              user=JGMYSQL_CONNEXT["MYSQL_USER"],
                              passwd=JGMYSQL_CONNEXT["MYSQL_PASSWD"],
                              charset=JGMYSQL_CONNEXT["MYSQL_CHARSET"])
    jg_cursor = jg_conn.cursor()
    sql = '''DELETE FROM %s WHERE IndexingFileID = "%s" AND Type = "1" AND DBName = "%s"''' % (taskname,sysid,DBName[dbname])
    try:
        jg_cursor.execute(sql)
    except Exception as e:
        print("删除校对状态出错：%s"%str(e))
        jg_cursor.close()
        jg_conn.close()
        return 0
    sql = '''UPDATE %s SET Status = "0", EndDate = NULL WHERE IndexingFileID = "%s" AND Type = "0" AND DBName = "%s"''' % (taskname,sysid,DBName[dbname])
    try:
        jg_cursor.execute(sql)
    except Exception as e:
        print("修改标引状态出错:%s"%str(e))
        jg_cursor.close()
        jg_conn.close()
        return 0
    sql = '''UPDATE %s SET %s = CONCAT(%s,"（标引错误，请重新标引该字段）") WHERE SYSID = "%s"''' % (dbname,fieldname,fieldname,sysid)
    try:
        jg_cursor.execute(sql)
    except Exception as e:
        print("修改原表数据值出错:%s"%str(e))
        jg_cursor.close()
        jg_conn.close()
        return 0
    jg_conn.commit()
    jg_cursor.close()
    jg_conn.close()
    return 1

def readfile(filepath, dbname, fieldname):
    sysid_list = []
    root,file  = os.path.split(filepath)
    with open(filepath, "r", encoding='utf-8') as f:
        for linen in f:
            line = linen.replace('\n', '')
            sysid_list.append(line)
    os.chdir(root)
    for sysid in sysid_list:
        result = delindexing(sysid,dbname, fieldname)
        if not result:
            with open("deleterro.txt", "a", encoding='utf-8') as f:
                f.write(sysid + '\n')

if __name__ == '__main__':
    filepath = "E:\工作记录\工作内容\“一带一路”项目\数据加工\需要重新标引的数据\重新标引.txt"
    filepath = os.path.join(filepath)
    dbname = "`case`"
    fieldname = "UseLaw"
    readfile(filepath, dbname, fieldname)


